/* $RCSFile: BooleanDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/BooleanDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Diagnostics;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// BooleanDataType
	/// </summary>
	public class BooleanDataType : BaseDataType
	{

		public BooleanDataType()
		{
			InnerDataType   =   PropertiesDataType.Boolean;
			Ok4Validators   =   false;
            innerValue      =   StStr.strFalse;
		}

		protected override void InitializeComponents()
		{
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]BooleanDataType InitializeComponents BEG ReadOnly: {0}",ReadOnly),DataTypeTraceSwitch.Sw.Info);
			CheckBox cb         =   new CheckBox();
            if ( ReadOnly )
                cb.Enabled      =   false;
			innerControl        =   cb;
            if (HandleChanges)
            {
                if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( "[V]BooleanDataType InitializeComponents HandleChanges set",DataTypeTraceSwitch.Sw.Info);
                cb.AutoPostBack = true;
                cb.CheckedChanged   +=  new EventHandler(Cb_Changed);
            }
            _IsInitialized  =   true;
		}
        
        protected void Cb_Changed(object sender, EventArgs e)
        {
    		if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]BooleanDataType Cb_Changed sender: {0}",sender),DataTypeTraceSwitch.Sw.Info);
            string oldValue         =   Val;
            ReadDataFromEditControl();
            string newValue         =   Val;
    		if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]BooleanDataType Cb_Changed oldValue: '{0}', newValue: '{1}'",oldValue,newValue),DataTypeTraceSwitch.Sw.Info);
            OnDataTypeEvent(new DataTypeEventArgs(DataTypeEventType.Changed,sender,e,oldValue,newValue));
        }

		public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				//Type check
				try
				{
				    innerValue = bool.Parse(value).ToString();
				}
				catch (Exception ex)
				{
                    if ( DataTypeTraceSwitch.Sw.TraceError )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]BooleanDataType Set Value: {0}, ex: {1}",value,ex),DataTypeTraceSwitch.Sw.Info);
                    throw;
				}
			}
		}

        public override void DataBind()
        {
			try
			{
				((CheckBox) innerControl).Checked =  bool.Parse(Val);
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]BooleanDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
        }
		
        /// <summary>
        /// Update value from control: used by eventhandler after a control change event triggered
        /// </summary>
        /// <returns></returns>
        public override  string ReadDataFromEditControl()
		{
            Control Ctl =   EditControl;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]BooleanDataType ReadDataFromEditControl BEG Control is null: {0}, Val: {1}.", (Ctl == null), Val ), DataTypeTraceSwitch.Sw.Info);
            if ( Ctl ==   null )
                return Val;
            if ( Ctl is CheckBox )
				innerValue = ((CheckBox)Ctl).Checked.ToString();
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]BooleanDataType ReadDataFromEditControl END InnerValue: {0}.", innerValue), DataTypeTraceSwitch.Sw.Info);
            return Val;
        }

        public override Control EditControl
		{
			get
			{
				if ( innerControl == null )
					InitializeComponents();
				return innerControl;
			}
			set
			{
                if ( value is CheckBox )
				{
                    if ( !innerControl.Equals(value) )
					    innerControl = value;
					//Update value from control
					innerValue = ((CheckBox) innerControl).Checked.ToString();
                    return;
				}
                string msg  =   "[E]BooleanDataType EditControl a CheckBox value is required, a '" + value.GetType().Name + "' is given.";
                if ( DataTypeTraceSwitch.Sw.TraceError )
                        Trace.WriteLine(msg,DataTypeTraceSwitch.Sw.Info);
				throw new ArgumentException(msg);
			}
		}

		public override string Description
		{
			get
			{
				return "Boolean";
			}
		}
	}
}